import numpy as np
import matplotlib.pyplot as plt
import sklearn.datasets
from sklearn import svm

# data sets
xy = sklearn.datasets.load_iris()
# xy = sklearn.datasets.load_breast_cancer()
print("xy: ", xy)

raw_x = xy.data
raw_y = xy.target

x = raw_x[raw_y != 2, 1:3]
y = raw_y[raw_y != 2]
print("x,y:",x,y)

# data shuffle
m = len(x)

np.random.seed(123)
p = np.random.permutation(m)
x, y = x[p], y[p]
print("x: ", x.shape)

# data visualization
plt.scatter(x[:, 0], x[:, 1], c = y)
plt.show()

# data split
train_size = 0.7
devide = int(train_size * m)
x_train, x_test = np.split(x, [devide])
y_train, y_test = np.split(y, [devide])

# model
clf = svm.SVC(C = 1, kernel='linear')
# model train
clf.fit(x_train, y_train)

# accurcy
h = clf.predict(x_test)
predict_y = np.where(h>0.5, 1, 0)
acc = np.mean(np.equal(predict_y, y_test))
print("accurcy: ", acc)

print("clf.coef_: ", clf.coef_)
print("clf.intercept_: ", clf.intercept_)
print("clf.support_vectors_: ", clf.support_vectors_)

# model parameters
theta = np.array([clf.intercept_[0], clf.coef_[0][0], clf.coef_[0][1]])

k = - theta[1] / theta[2]
b = - theta[0] / theta[2]

x1 = np.linspace(1.5, 4.5)
x2 = k * x1 + b

down = clf.support_vectors_[0]
up = clf.support_vectors_[-1]

x2_down = k * (x1 - down[0]) + down[1]
x2_up = k * (x1 - up[0]) + up[1]

# plt.figure(figsize=(8, 4))
plt.plot(x1, x2)
plt.plot(x1, x2_down, '--')
plt.plot(x1, x2_up, '--')
plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1])
plt.scatter(x[:, 0], x[:, 1], c = y)
plt.show()
